package com.programmercarl.tree;

/**
 * https://leetcode.cn/problems/sum-of-left-leaves/description/
 *
 * @author yulongtian
 * @create 2024-12-19 9:36
 */
public class Test10 {
    public static void main(String[] args) {

    }

    public class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;

        TreeNode() {
        }

        TreeNode(int val) {
            this.val = val;
        }

        TreeNode(int val, TreeNode left, TreeNode right) {
            this.val = val;
            this.left = left;
            this.right = right;
        }
    }

    public int sumOfLeftLeaves(TreeNode root) {
        int[] res = new int[1];

        dfs(root, res);

        return res[0];
    }

    public void dfs(TreeNode root, int[] sumLeft) {
        if (root == null) {
            return;
        }
        if (root.left != null && root.left.left == null && root.left.right == null) {
            sumLeft[0] += root.left.val;
        }
        dfs(root.left, sumLeft);
        dfs(root.right, sumLeft);
    }

}
